/*
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 *
 */

import Loadable from "react-loadable";
import * as React from "react";
import { Switch, Redirect } from "react-router-dom";
import Authorized from "../components/Authorized";
import menuData, { IMenu } from "./menu";
import Loading from "../components/Loading";
import { homePage } from "../config/base";

/**
 * 路由配置
 * authority:权限控制，可以为一组权限数组，也可以为字符串，为字符串时多个权限用逗号隔开，
 * component:页面组件路径，
 * path:路由路径
 */
export interface IRouter {
  authority?: string[] | string; // 权限控制
  component: string; // 组件地址
  path: string; // 路由地址
  name?: string; // 名称
}

// 配置路由
export const routes: IRouter[] = [
  {
    component: "Goods/components/goodsManage",
    path: "/goods",
    name: "商品管理"
  },
  {
    component: "Goods/components/goodsManage/GoodsAdd",
    path: "/goods/goodsAdd",
    name: "商品添加"
  },
  {
    component: "Goods/components/goodsManage/GoodsEdit",
    path: "/goods/goodsEdit",
    name: "商品修改"
  },
  {
    component: "Goods/components/goodsManage/GoodsList",
    path: "/goods/goodsList",
    name: "商品查询"
  },
  {
    component: "Goods/components/goodsManage/GoodsDetail",
    path: "/goods/goodsDetail",
    name: "商品查看"
  },
  {
    component: "Goods/components/goodsManage/GoodsPair",
    path: "/goods/goodsPair",
    name: "防伪码配对"
  },
  {
    component: "Security/components/securityManage",
    path: "/security",
    name: "防伪码"
  },
  {
    component: "Security/components/securityManage/SecurityAdd",
    path: "/security/securityAdd",
    name: "防伪码生成"
  },
  {
    component: "Security/components/securityManage/SecurityDel",
    path: "/security/securityDel",
    name: "防伪码删除"
  },
  {
    component: "Security/components/securityManage/SecuritySend",
    path: "/security/securitySend",
    name: "防伪码批量下发"
  },
  {
    component: "Product/components/productManage",
    path: "/product",
    name: "产品管理"
  },
  {
    component: "Product/components/productManage/ProductAdd",
    path: "/product/productAdd",
    name: "产品类别管理"
  },
  {
    component: "Product/components/productManage/ProductEdit",
    path: "/product/productEdit",
    name: "产品类别修改"
  },
  {
    component: "Product/components/productManage/ProductList",
    path: "/product/productList",
    name: "产品类别列表"
  },
  {
    component: "Product/components/productManage/ProductDetail",
    path: "/product/productDetail",
    name: "产品类别查看"
  },
  {
    component: "SearchLog/components/searchLogManage",
    path: "/searchLog",
    name: "查询统计"
  },
  {
    component: "SearchLog/components/searchLogManage/SearchLogList",
    path: "/searchLog/searchLogList",
    name: "防伪查询统计"
  },
  {
    component: "SearchLog/components/searchLogManage/SearchLogHandle",
    path: "/searchLog/searchLogHandle",
    name: "防伪查询统计删除"
  },
  {
    component: "SystemManage/components/systemManage",
    path: "/systemManage",
    name: "系统管理"
  },
  {
    component: "User/components/userManage/UserList",
    path: "/systemManage/userList",
    name: "用户管理"
  },
  {
    component: "Role/components/roleManage/RoleUserList",
    path: "/systemManage/roleList",
    name: "权限管理"
  }
];

// 根据菜单设置路由
function getMenuData(menu: IMenu[], router: IRouter[], path?: string) {
  menu.forEach((item: IMenu) => {
    if (item.children) {
      getMenuData(item.children, router, item.path);
    } else {
      for (const ele of routes) {
        if (
          !path ||
          (item.authority && !ele.authority && path + item.path === ele.path)
        ) {
          ele.authority = item.authority;
        }
      }
    }
  });
}

/**
 * 根据路由配置创建路由组件
 * @param {Array<any>} routerArray
 */

function getRoutes(routerArray: IRouter[]) {
  getMenuData(menuData, routerArray);
  return routerArray.map((item: IRouter) => {
    const LoadableComponent = Loadable({
      loader: () => import(`../pages/${item.component}`),
      loading() {
        return <Loading />;
      }
    });
    if (item.authority) {
      return (
        <Authorized
          key={item.path}
          exact={true}
          path={item.path}
          authority={item.authority}
          component={LoadableComponent}
        />
      );
    }
    return (
      <Authorized
        key={item.path}
        exact={true}
        path={item.path}
        component={LoadableComponent}
      />
    );
  });
}
const routeCache = getRoutes(routes);
class Router extends React.Component {
  public render() {
    return (
      <Switch>
        {routeCache}
        {location.pathname === "/" ? <Redirect to={homePage} /> : null}
        <Authorized
          key="exception"
          component={Loadable({
            loader: () => import(`../pages/Index`),
            loading() {
              return <Loading />;
            }
          })}
        />
      </Switch>
    );
  }
}

export default Router;
